﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using RibbonEditor.AppCode;
using RibbonEditor.Forms;

namespace RibbonEditor.Rules
{
    public class OrRule : IEnableRule, IDisplayRule, IXmlBuildable, IChildDeletable
    {
        public List<OrGroup> Ors { get; set; }

        public OrRule()
        {
            this.Ors = new List<OrGroup>();
        }

        public OrRule(XmlNode orRuleXmlNode) : this()
        {
            try
            {
                foreach (XmlNode orXmlNode in orRuleXmlNode.ChildNodes)
                {
                    if (orXmlNode.NodeType == XmlNodeType.Comment)
                        return;

                    OrGroup og = new OrGroup(orXmlNode);
                    this.Ors.Add(og);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void AddNode(XmlNode ruleNode)
        {
            XmlNode orRuleNode = XmlHelper.AddNode(ruleNode, "OrRule");

            foreach (OrGroup og in this.Ors)
            {
                og.AddNode(orRuleNode);

            }
        }

        public void BuildTreeViewElement(TreeNode parentNode)
        {
            try
            {
                TreeNode orRuleNode = new TreeNode("OrRule");
                orRuleNode.Tag = this;
                orRuleNode.ContextMenuStrip = this.BuildContextMenuStrip(orRuleNode);

                if (this.Ors != null)
                {
                    foreach (OrGroup og in this.Ors)
                    {
                        og.BuildTreeViewElement(orRuleNode);
                    }
                }

                parentNode.Nodes.Add(orRuleNode);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private ContextMenuStrip BuildContextMenuStrip(TreeNode relatedNode)
        {
            ContextMenuStrip cms = new ContextMenuStrip();
            cms.ItemClicked += new ToolStripItemClickedEventHandler(cms_ItemClicked);
            cms.Tag = relatedNode;

            ToolStripMenuItem tsmi = new ToolStripMenuItem("Add Rule Group");
            ToolStripMenuItem tsmdx = new ToolStripMenuItem("Display Xml");
            ToolStripSeparator tsms = new ToolStripSeparator();
            ToolStripMenuItem tsmd = new ToolStripMenuItem("Delete");

            cms.Items.Add(tsmi);
            cms.Items.Add(tsms);
            cms.Items.Add(tsmdx);
            cms.Items.Add(tsms);
            cms.Items.Add(tsmd);

            return cms;
        }

        void cms_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            TreeNode selectedNode = (TreeNode)e.ClickedItem.GetCurrentParent().Tag;

            switch (e.ClickedItem.Text)
            {
                case "Display Xml":
                    {
                        XmlDocument doc = new XmlDocument();
                        doc.LoadXml("<root></root>");

                        ((IXmlBuildable)selectedNode.Tag).AddNode(doc.DocumentElement);

                        XmlContentDisplayDialog dialog = new XmlContentDisplayDialog(doc.DocumentElement.InnerXml);
                        dialog.StartPosition = FormStartPosition.CenterParent;
                        dialog.ShowDialog();
                    }
                    break;
                case "Delete":
                    {
                        selectedNode.TreeView.SelectedNode = selectedNode.Parent;
                        UIActionHelper.Delete(selectedNode);
                        //selectedNode.Parent.Nodes.Remove(selectedNode);

                    }
                    break;
                case "Add Rule Group":
                    {
                        OrGroup oGroup = new OrGroup();
                        oGroup.BuildTreeViewElement(selectedNode);
                    }
                    break;
            }
        }

        #region IChildDeletable Members

        public void DeleteChild(object o)
        {
            if (o is OrGroup)
            {
                this.Ors.Remove((OrGroup)o);
            }
            else
            {
                throw new Exception("Unsupported child type: " + o.GetType());
            }
        }

        #endregion
    }
}
